//
//  LoginViewController.m
//  PT
//
//  Created by jarze on 15/6/8.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "LoginViewController.h"
#import "MBProgressHUD+NJ.h"
#import "LoginViewController.h"


@interface LoginViewController ()
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 通知监听label的改变
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(textChangeField) name:UITextFieldTextDidChangeNotification object:self.account];
    
    [center addObserver:self selector:@selector(textChangeField) name:UITextFieldTextDidChangeNotification object:self.password];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textChangeField
{
//    // 3. 回显数据(读取保存的偏好设置信息)
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    if (
//    // 设置账号
//    self.account.text == [defaults objectForKey:@"account"] &&
//    // 设置密码
//    self.password.text == [defaults objectForKey:@"password"]
//        ){
    

//    userName
    // 登录按钮的enable
//    self.login.enabled = [NSString checkUserEmail:self.account.text] && [NSString checkPassword:self.password.text];//}
    
    if ([NSString checkUserEmail:self.account.text] && [NSString checkPassword:self.password.text]) {
    
    //以下为测试用代码，完成后直接删除
    self.login.enabled = YES;
    }
    
}



- (void)viewDidAppear:(BOOL)animated
{
    // 主动召唤键盘
    [self.account becomeFirstResponder];
    
}


- (IBAction)loginBtn:(id)sender {

//        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//   
//    
    
//        NSString *home = NSHomeDirectory();
//        NSLog(@"%@", home);


        


         //添加蒙版禁止用户操作, 并且提示用户正在登录
        [MBProgressHUD showMessage:@"正在拼命加载ing...."];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
    //        if (![self.account.text isEqualToString:[defaults objectForKey:@"account"]] && ![self.password.text isEqualToString:[defaults objectForKey:@"password"]]) {
    //            // 3.登录成功后移除蒙版
    //            [MBProgressHUD hideHUD];
    //            [MBProgressHUD showError:@"账号与密码不符，请重新输入"];
    //            return;
    //        }
           
            // 3.登录成功后移除蒙版
            
            [MBProgressHUD hideHUD];
        

      
    //    判断账号密码是否正确
    //    1.不正确，提示：“账号与密码不符，请重新输入”
    //    
    //    2.如果正确，判断是否已领取免费预约
        
        //没有领取，跳转到免费领取界面(手动执行segue)
//            [self performSegueWithIdentifier:@"longinToReceive" sender:nil];
            
//获取storyboard: 通过bundle根据storyboard的名字来获取我们的storyboard,
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//由storyboard根据myView的storyBoardID来获取我们要切换的视图
            UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"myView"];
            NSMutableArray *array = [NSMutableArray arrayWithArray: self.navigationController.childViewControllers];
            [array removeAllObjects];
            [array addObject:myView];
            self.navigationController.viewControllers = array;

       // 已经领取， 跳转到约课页面
        
        // self performSegueWithIdentifier:<#(NSString *)#> sender:nil];
        
            
    //    });
        });
    
    

}
                   
@end
